package com.sto.stoapp.activity.scanner.inquiry;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.sto.stoapp.dataprocess.ProcessTableData;
import com.sto.stoapp.db.StoTableName;
import com.sto.stoapp.entity.StatisticsEntity;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.status.EnumTableNames;
import com.sto.stoapp.util.UnifiedStaticType;

public class InquiryStatistics {
	private Context context;
	private InquiryQueryTable querytable;
	private StoTableName stoTableName;
	private List<StatisticsEntity> statisticsList = new ArrayList<StatisticsEntity>();

	public InquiryStatistics(Context context1, StoTableName stoTableName) {
		context = context1;
		this.stoTableName = stoTableName;
	}

	/**
	 * 统计每一表的已未发数据个数
	 * 
	 * @return
	 */
	public List<StatisticsEntity> statisticsDataStatus(InquiryFieldSql fieldSql) {
		try {
			String date = fieldSql.getInquiryField()[0];// 获取日期
			querytable = new InquiryQueryTable(context);
			ProcessTableData tableData = new ProcessTableData();
			int item = tableData.getInquiryMaxItem();
			
			for (int i = 1; i < item - 1; i++) {
				EnumTableNames tables = EnumTableNames.values()[tableData.getProcessInquiryData(i, 1)];
//				StatisticsCount(
//						stoTableName.getTableName[tableData.getProcessInquiryData(
//								i, 1)], date);
				StatisticsCount(new String[]{tables.getTableName(), tables.getScanTypeName()}, date);
			}

		} catch (Exception e) {
			e.printStackTrace();
			ExceptionLog.writeLog("statisticsDataStatus", e.getMessage());
		}

		return statisticsList;
	}

	/**
	 * 统计已未发数据个数(元数据处理个数 where 1)
	 * 
	 * @param tablename
	 * @param date
	 */
	private void StatisticsCount(String[] tablename, String date) {
		int yes_count = 0, no_count = 0;

		StatisticsEntity entity = new StatisticsEntity();

		yes_count = querytable.StatisticsSentCount(tablename[0], date,
				UnifiedStaticType.YES_STATUS);
		no_count = querytable.StatisticsSentCount(tablename[0], date,
				UnifiedStaticType.NO_STATUS);
		entity.setUnSendCount(context.getString(Integer.parseInt(tablename[1]))
				+ ": " + "已发 " + String.valueOf(yes_count));
//		//entity.setUnSendCount(tablename[1] + ": " + "已发 " + String.valueOf(yes_count));
		entity.setSendCount("未发 " + String.valueOf(no_count));
		statisticsList.add(entity);
	}
}
